package com.activiti.flow.controller;

import com.activiti.basic.exception.ApprovalException;
import com.activiti.basic.model.ResponseUtil;
import com.activiti.basic.model.WebResponse;
import com.activiti.flow.model.vo.ProcessDefinitionVO;
import com.activiti.flow.model.vo.ProcessInstanceVO;
import com.activiti.flow.service.ProcessInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pisory
 * @version 1.0
 * @date 2020/4/6
 */
@Api(tags = "流程实例管理")
@RestController
@RequestMapping("/processInstance")
public class ProcessInstanceController {

    @Autowired
    private ProcessInstanceService processInstanceService;


    @ApiOperation("启动流程")
    @PostMapping("/startProcess")
    public WebResponse<ProcessInstance> startProcess(@RequestBody ProcessDefinitionVO vo) throws Exception{
        ProcessInstance processInstance = processInstanceService.startProcessByKey(vo);
        return ResponseUtil.success();
    }

    @GetMapping
    public WebResponse<List<ProcessInstance>> listProcessInstance(){

        return ResponseUtil.success();
    }

    @ApiOperation("通过流程")
    @PostMapping("/pass")
    public WebResponse pass(@RequestBody ProcessInstanceVO vo) throws ApprovalException {

        validated(vo);

        int i = processInstanceService.pass(vo);
        if(i == 0) {
            throw new ApprovalException("审批失败");
        }

        return ResponseUtil.success("通过成功");
    }

    /**
     * 校验数据
     * @param vo
     */
    private void validated(ProcessInstanceVO vo) {

    }
}

